#!/bin/sh
#
# rocketmq - this script starts and stops the rocketmq daemon
#
# chkconfig: - 85 15

export JAVA_HOME={{jdk_base_path}}
export PATH=$JAVA_HOME/bin:$PATH
 
ROCKETMQ_HOME={{ rocketmq_base }}
ROCKETMQ_BIN=${ROCKETMQ_HOME}/bin
LOG_DIR=${ROCKETMQ_HOME}/logs
CONF_FILE={{ rocketmq_base }}/conf/{{brokername}}.properties
BROKER_LOG=${LOG_DIR}/broker.log
 
start() {
    if [ ! -d ${LOG_DIR} ];then
        mkdir ${LOG_DIR}
    fi
    cd ${ROCKETMQ_HOME}
    nohup sh bin/mqbroker -c ${CONF_FILE}  > ${BROKER_LOG} 2>&1 &
    echo -n "The broker boot success..."
}
stop() {
    pid=`ps ax |grep {{brokername}}|grep -i 'org.apache.rocketmq.broker.BrokerStartup' |grep java | grep -v grep | awk '{print $1}'`
    if [ -z "$pid" ] ; then
            echo "No mqbroker running."
    fi

    echo "The mqbroker(${pid}) is running..."

    kill ${pid}

    echo "Send shutdown request to mqbroker(${pid}) OK"
}
restart() {
    stop
    sleep 5
    start
}
 
 
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 2
	;;
esac
